package com.ggh.jinjilive.ui.mine.address.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemAddressBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.goods.thegoods.TheGoodsAddressMobile;

import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private TheGoodsAddressMobile mTheGoodsAddressMobile;
    private String distinguish;
    public AddressAdapter(TheGoodsAddressMobile mTheGoodsAddressMobile,String distinguish){
        this.mTheGoodsAddressMobile = mTheGoodsAddressMobile;
        this.distinguish = distinguish;
    }

    public AddressAdapter(String distinguish){
        this.distinguish = distinguish;
    }


    private List<TheGoodsAddressMobile> theGoodsAddressMobileList;

    public void setAdd(List<TheGoodsAddressMobile> theGoodsAddressMobileList) {
        this.theGoodsAddressMobileList = theGoodsAddressMobileList;
        notifyDataSetChanged();
    }


    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemAddressBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_address, parent, false);
        return new ItemViewHolder(binding);
    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
        TheGoodsAddressMobile theGoodsAddressMobile = theGoodsAddressMobileList.get(position);
        ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
        if(null != theGoodsAddressMobile){
            //添加收货地址
            holder.binding.llAddAddress.setVisibility(View.GONE);
            //收货地址
            holder.binding.llTheGoods.setVisibility(View.VISIBLE);
            if(null != distinguish && distinguish.equals("确认订单")){
                if(null != mTheGoodsAddressMobile){
                    //处理当前是否选中
                    if(mTheGoodsAddressMobile.getId() == theGoodsAddressMobile.getId()){
                        holder.binding.imgPitchup.setVisibility(View.VISIBLE);
                        holder.binding.useButton.setVisibility(View.GONE);
                    }else {
                        holder.binding.imgPitchup.setVisibility(View.GONE);
                        holder.binding.useButton.setVisibility(View.VISIBLE);
                    }
                }else {
                    holder.binding.imgPitchup.setVisibility(View.GONE);
                    holder.binding.useButton.setVisibility(View.VISIBLE);
                }
                //使用点击
                holder.binding.useButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(null != addItemOnClickListener){
                            addItemOnClickListener.onSkipClicked(9997,theGoodsAddressMobile);
                        }
                    }
                });
            }else {
                holder.binding.imgPitchup.setVisibility(View.INVISIBLE);
                holder.binding.useButton.setVisibility(View.INVISIBLE);
            }

            if(theGoodsAddressMobile.getIs_default() == 1){
                holder.binding.maleButtonId.setText("已设为默认");
                holder.binding.maleButtonId.setTextColor(Color.parseColor("#1890ff"));
                holder.binding.maleButtonId.setChecked(true);
            }else {
                holder.binding.maleButtonId.setText("设为默认");
                holder.binding.maleButtonId.setTextColor(Color.parseColor("#bfbfbf"));
                holder.binding.maleButtonId.setChecked(false);
            }

            //姓名和手机号
            holder.binding.tvNameCellphone.setText(theGoodsAddressMobile.getName() + "," + theGoodsAddressMobile.getMobile());
            //详情地址
            holder.binding.tvAddress.setText(theGoodsAddressMobile.getContent() + theGoodsAddressMobile.getS_content());
            //设为默认
            holder.binding.maleButtonId.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        if(holder.binding.maleButtonId.isChecked()){
                            theGoodsAddressMobile.setIs_default(1);
                        }else {
                            theGoodsAddressMobile.setIs_default(0);
                        }
                        addItemOnClickListener.onSkipClicked(9998,theGoodsAddressMobile);
                    }
                }
            });
            //删除
            holder.binding.tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(9996,theGoodsAddressMobile);
                    }
                }
            });
            //修改
            holder.binding.tvModification.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(9995,theGoodsAddressMobile);
                    }
                }
            });



        }else {
            //添加收货地址
            holder.binding.llAddAddress.setVisibility(View.VISIBLE);
            holder.binding.llAddAddress.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(9999,null);
                    }
                }
            });
            //收货地址
            holder.binding.llTheGoods.setVisibility(View.GONE);

        }

    }

    @Override
    public int getItemCount() {
        return null == theGoodsAddressMobileList ? 0 : theGoodsAddressMobileList.size();
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemAddressBinding binding;
        public ItemViewHolder(@NonNull ItemAddressBinding binding) {
            super(binding.getRoot());
            this.binding = binding;

        }
    }

}